//
//  HBBadgeView.m
//  HBLibary
//
//  Created by Rambo on 2018/11/4.
//

#import "HBBadgeView.h"
#import "NSString+HBCategory.h"


static NSInteger const kMaxCount = 99;
static NSString * const kAddSymbol = @"+";

@interface HBBadgeView ()

@property (nonatomic, assign) HBBadgeViewStyle style;

@property (nonatomic, strong) UILabel *label;

@end

@implementation HBBadgeView

- (instancetype)initWithPresentView:(UIView *)presentView style:(HBBadgeViewStyle)style{
    if (self = [super init]) {
        self.style = style;
        self.label = [[UILabel alloc] init];
        self.label.font = [UIFont systemFontOfSize:10];
        self.label.textColor = [UIColor redColor];
        self.layer.borderColor = [UIColor redColor].CGColor;
        self.layer.borderWidth = 0.5;
        //        self.layer.masksToBounds = YES;
        self.label.textAlignment = NSTextAlignmentCenter;
        self.backgroundColor = [UIColor redColor];
        self.hidden = YES;
        [self addSubview:self.label];
        _cornerRadius = -1;
        self.countColor = [UIColor whiteColor];
        _countInsets = UIEdgeInsetsMake(3, 2, 3, 2);
        [presentView addSubview:self];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self updateFrame];
}

#pragma mark - set

- (void)setBadgeCount:(NSInteger)badgeCount{
    _badgeCount = badgeCount;
    if (_badgeCount < 0) {
        _badgeCount = 0;
    }
    self.hidden = (_badgeCount == 0);
    [self updateFrame];
}

- (void)updateFrame{
    [self.superview setNeedsLayout];
    NSString *countString = [NSString stringWithFormat:@"%ld",_badgeCount];
    if (_badgeCount > kMaxCount) {
        countString = [NSString stringWithFormat:@"%ld%@",kMaxCount,kAddSymbol];
    }
    
    CGFloat baselineOffset = 3.0f;
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:countString attributes:@{NSFontAttributeName : self.label.font, NSForegroundColorAttributeName : self.label.textColor}];
    NSRange range = [countString rangeOfString:kAddSymbol];
    if (range.location != NSNotFound) {
        [attString addAttributes:@{NSBaselineOffsetAttributeName : @(baselineOffset)} range:range];
    }
    //
    self.label.attributedText = attString;
    
    //    self.text = badgeString;
    
    CGSize size = [countString hb_sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) font:self.label.font];
    size = CGSizeMake(size.width + self.countInsets.left + self.countInsets.right, size.height + self.countInsets.top + self.countInsets.bottom);
    CGRect rect = self.frame;
    rect.size.width = MAX(size.width, size.height);
    rect.size.height = size.height;
    rect.origin.y = -size.height*0.5 + self.badgePositionAdjustment.y;
    if (self.cornerRadius < 0) {
        self.layer.cornerRadius = size.height * 0.5;
    }else{
        self.layer.cornerRadius = self.cornerRadius;
    }
    
    if (self.style == HBBadgeViewStyleLeft) {
        rect.origin.x = -size.width * 0.5 - self.badgePositionAdjustment.x;
    }else if (self.style == HBBadgeViewStyleRight){
        rect.origin.x = self.superview.frame.size.width - size.width * 0.5 + self.badgePositionAdjustment.x;
    }
    self.frame = rect;
    self.label.frame = self.bounds;
    if (_badgeCount > kMaxCount) {
        self.label.frame = CGRectMake(0, -(baselineOffset*0.5), self.frame.size.width, self.frame.size.height);
    }
    
}

- (void)setBadgePositionAdjustment:(CGPoint)badgePositionAdjustment{
    _badgePositionAdjustment = badgePositionAdjustment;
    [self updateFrame];
}

- (void)setCountColor:(UIColor *)countColor{
    _countColor = countColor;
    self.label.textColor = _countColor;
}

- (void)setBorderColor:(UIColor *)borderColor{
    _borderColor = borderColor;
    self.layer.borderColor = _borderColor.CGColor;
}

- (void)setCornerRadius:(CGFloat)cornerRadius{
    _cornerRadius = cornerRadius;
    
    if (_cornerRadius < 0) {
        self.layer.cornerRadius = self.frame.size.height * 0.5;
    }else{
        self.layer.cornerRadius = _cornerRadius;
    }
}

- (void)setCountInsets:(UIEdgeInsets)countInsets{
    _countInsets = countInsets;
    [self updateFrame];
}

@end
